﻿using Engine.Graphics;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Scenario_Editor.Windows
{
    public partial class MaterialAssignWindows : Form
    {
        #region Private Fields

        private MaterialManager materialManager;
        private Material newMaterial;
        private Material originalMaterial;
        private Material pickedMaterial;

        #endregion Private Fields

        #region Public Constructors

        public MaterialAssignWindows()
        {
            InitializeComponent();
        }

        #endregion Public Constructors

        // Public Methods (4) 

        #region Public Methods

        public Material GetNewMaterial()
        {
            return this.newMaterial;
        }

        public void SetCurrentMaterial(Material currentMaterial)
        {
            this.originalMaterial = currentMaterial;
            this.newMaterial = currentMaterial;

            UpdateNewMaterialControls();
        }

        public void SetMaterialManager(MaterialManager materialManager)
        {
            this.materialManager = materialManager;
        }

        public void SetPickedupMateiral(Material pickedUpMaterial)
        {
            this.pickedMaterial = pickedUpMaterial;
            UpdatePickedMaterialControls();
        }

        #endregion Public Methods

        // Private Methods (16) 

        #region Private Methods

        private void button_AssignAllTextures_Click(object sender, EventArgs e)
        {
            this.newMaterial.TextureName1 = this.pickedMaterial.TextureName1;
            this.newMaterial.Mappings1 = this.pickedMaterial.Mappings1;

            this.newMaterial.TextureName2 = this.pickedMaterial.TextureName2;
            this.newMaterial.Mappings2 = this.pickedMaterial.Mappings2;

            this.newMaterial.TextureName3 = this.pickedMaterial.TextureName3;
            this.newMaterial.Mappings3 = this.pickedMaterial.Mappings3;

            UpdateNewMaterialControls();
        }

        private void button_Reset_Click(object sender, EventArgs e)
        {
            this.newMaterial = this.originalMaterial;
            UpdateNewMaterialControls();
        }

        private void button_Switch1with2_Click(object sender, EventArgs e)
        {
            Mappings tempMappings = this.newMaterial.Mappings1;
            this.newMaterial.Mappings1 = this.newMaterial.Mappings2;
            this.newMaterial.Mappings2 = tempMappings;

            string tempTextureName = this.newMaterial.TextureName1;
            this.newMaterial.TextureName1 = this.newMaterial.TextureName2;
            this.newMaterial.TextureName2 = tempTextureName;

            UpdateNewMaterialControls();
        }

        private void button_Switch2with3_Click(object sender, EventArgs e)
        {
            Mappings tempMappings = this.newMaterial.Mappings3;
            this.newMaterial.Mappings3 = this.newMaterial.Mappings2;
            this.newMaterial.Mappings2 = tempMappings;

            string tempTextureName = this.newMaterial.TextureName3;
            this.newMaterial.TextureName3 = this.newMaterial.TextureName2;
            this.newMaterial.TextureName2 = tempTextureName;

            UpdateNewMaterialControls();
        }

        private void button_Textur1To1_Click(object sender, EventArgs e)
        {
            this.newMaterial.TextureName1 = this.pickedMaterial.TextureName1;
            this.newMaterial.Mappings1 = this.pickedMaterial.Mappings1;
            UpdateNewMaterialControls();
        }

        private void button_Textur1To2_Click(object sender, EventArgs e)
        {
            this.newMaterial.TextureName2 = this.pickedMaterial.TextureName1;
            this.newMaterial.Mappings2 = this.pickedMaterial.Mappings1;
            UpdateNewMaterialControls();
        }

        private void button_Textur1To3_Click(object sender, EventArgs e)
        {
            this.newMaterial.TextureName3 = this.pickedMaterial.TextureName1;
            this.newMaterial.Mappings3 = this.pickedMaterial.Mappings1;
            UpdateNewMaterialControls();
        }

        private void button_Textur2To1_Click(object sender, EventArgs e)
        {
            this.newMaterial.TextureName1 = this.pickedMaterial.TextureName2;
            this.newMaterial.Mappings1 = this.pickedMaterial.Mappings2;
            UpdateNewMaterialControls();
        }

        private void button_Textur2To2_Click(object sender, EventArgs e)
        {
            this.newMaterial.TextureName2 = this.pickedMaterial.TextureName2;
            this.newMaterial.Mappings2 = this.pickedMaterial.Mappings2;
            UpdateNewMaterialControls();
        }

        private void button_Textur2To3_Click(object sender, EventArgs e)
        {
            this.newMaterial.TextureName3 = this.pickedMaterial.TextureName2;
            this.newMaterial.Mappings3 = this.pickedMaterial.Mappings2;
            UpdateNewMaterialControls();
        }

        private void button_Textur3To1_Click(object sender, EventArgs e)
        {
            this.newMaterial.TextureName1 = this.pickedMaterial.TextureName3;
            this.newMaterial.Mappings1 = this.pickedMaterial.Mappings3;
            UpdateNewMaterialControls();
        }

        private void button_Textur3To2_Click(object sender, EventArgs e)
        {
            this.newMaterial.TextureName2 = this.pickedMaterial.TextureName3;
            this.newMaterial.Mappings2 = this.pickedMaterial.Mappings3;
            UpdateNewMaterialControls();
        }

        private void button_Textur3To3_Click(object sender, EventArgs e)
        {
            this.newMaterial.TextureName3 = this.pickedMaterial.TextureName3;
            this.newMaterial.Mappings3 = this.pickedMaterial.Mappings3;
            UpdateNewMaterialControls();
        }

        private void SetPicture(PictureBox pictureBox, string fileName)
        {
            Ptx2bmpConverter.Ptx2bmp.PtxType typeOfPTX;

            try
            {
                Bitmap image = Ptx2bmpConverter.Ptx2bmp.FromFile(fileName, out typeOfPTX);

                // Update the Panel
                if (image.Size.Width > pictureBox.ClientSize.Width)
                {
                    pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
                }
                else
                {
                    pictureBox.SizeMode = PictureBoxSizeMode.Normal;
                }
                pictureBox.Image = image;
            }
            catch (Exception ex)
            {
                pictureBox.Image = pictureBox.ErrorImage;
                MessageBox.Show(ex.Message);
            }
        }

        private void UpdateNewMaterialControls()
        {
            int textureID1, textureID2, textureID3;
            string texturePath;
            int indexOfMaterial;

            indexOfMaterial = materialManager.GetMaterialID(this.newMaterial);

            textureID1 = this.materialManager.GetTextureID(this.newMaterial.TextureName1);
            textureID2 = this.materialManager.GetTextureID(this.newMaterial.TextureName2);
            textureID3 = this.materialManager.GetTextureID(this.newMaterial.TextureName3);

            if (textureID1 != -1)
            {
                texturePath = this.materialManager.TexturePaths[textureID1];
                SetPicture(this.pictureBox_newTexture1, texturePath);
            }

            if (textureID2 != -1)
            {
                texturePath = this.materialManager.TexturePaths[textureID2];
                SetPicture(this.pictureBox_newTexture2, texturePath);
            }

            if (textureID3 != -1)
            {
                texturePath = this.materialManager.TexturePaths[textureID3];
                SetPicture(this.pictureBox_newTexture3, texturePath);
            }
        }

        private void UpdatePickedMaterialControls()
        {
            int textureID1, textureID2, textureID3;
            string texturePath;
            int indexOfMaterial;

            indexOfMaterial = materialManager.GetMaterialID(pickedMaterial);

            textureID1 = this.materialManager.GetTextureID(pickedMaterial.TextureName1);
            textureID2 = this.materialManager.GetTextureID(pickedMaterial.TextureName2);
            textureID3 = this.materialManager.GetTextureID(pickedMaterial.TextureName3);

            if (textureID1 != -1)
            {
                texturePath = this.materialManager.TexturePaths[textureID1];
                SetPicture(this.pictureBox_Texture1, texturePath);
            }

            if (textureID2 != -1)
            {
                texturePath = this.materialManager.TexturePaths[textureID2];
                SetPicture(this.pictureBox_Texture2, texturePath);
            }

            if (textureID3 != -1)
            {
                texturePath = this.materialManager.TexturePaths[textureID3];
                SetPicture(this.pictureBox_Texture3, texturePath);
            }
        }

        #endregion Private Methods
    }
}